﻿using System.Collections.Generic;
using System.Net;
using System.Windows;
using Campeonato.Model;
using Microsoft.Phone.Controls;
using Newtonsoft.Json;
using PESWindowsPhone.Util;

namespace PESWindowsPhone
{
    public partial class MainPage : PhoneApplicationPage
    {
        private DataRequest dataRequest;

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }

            dataRequest = new DataRequest();
            dataRequest.DataReceived += OnDataRequestedReceived;
        }

        private void OnDataRequestedReceived(object sender, DownloadStringCompletedEventArgs e)
        {
            RankingList.ItemsSource = JsonConvert.DeserializeObject<IList<Jogador>>(e.Result);            
        }
    }
}